package com.anxin.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Knife4jConfig {

    // API标题
    private static final String API_TITLE = "安心医疗系统 API 文档";
    // API描述
    private static final String API_DESCRIPTION = "安心医疗预约挂号系统后端API文档";
    // API版本
    private static final String API_VERSION = "1.0.0";
    // 授权头名称
    private static final String SECURITY_HEADER = "Authorization";
    // 授权方案名称
    private static final String SECURITY_SCHEME = "bearer";
    // 授权承载令牌类型
    private static final String SECURITY_BEARER_FORMAT = "JWT";

    @Bean
    public OpenAPI apiInfo() {
        return new OpenAPI()
                .info(new Info().title(API_TITLE)
                        .description(API_DESCRIPTION)
                        .version(API_VERSION))
                .addSecurityItem(new SecurityRequirement().addList(SECURITY_HEADER))
                .schemaRequirement(SECURITY_HEADER, new SecurityScheme()
                        .name(SECURITY_HEADER)
                        .type(SecurityScheme.Type.HTTP)
                        .scheme(SECURITY_SCHEME)
                        .bearerFormat(SECURITY_BEARER_FORMAT)
                        .in(SecurityScheme.In.HEADER)
                        .description("JWT认证token，格式：Bearer {token}"));
    }

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("公共接口")
                .packagesToScan("com.anxin.controller")
                .addOpenApiCustomizer(openApi -> {
                    // 添加全局安全配置
                    openApi.addSecurityItem(new SecurityRequirement().addList(SECURITY_HEADER));
                })
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("系统管理接口")
                .packagesToScan("com.anxin.system.controller.admin")
                .addOpenApiCustomizer(openApi -> {
                    // 添加全局安全配置
                    openApi.addSecurityItem(new SecurityRequirement().addList(SECURITY_HEADER));
                })
                .build();
    }

    // 添加时间戳请求头说明
    private OpenApiCustomizer timeStampHeader() {
        return openApi -> openApi.getPaths().values().stream()
                .flatMap(pathItem -> pathItem.readOperations().stream())
                .forEach(operation -> operation.addParametersItem(
                        new io.swagger.v3.oas.models.parameters.Parameter()
//                                .name("Authorization")
//                                .description("请求时间戳(毫秒)")
//                                .in("header")
//                                .required(true)
//                                .schema(new io.swagger.v3.oas.models.media.Schema<>().type("long"))
                ));
    }
}
